package com.phpxiu.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;

import com.phpxiu.app.adapter.holder.C2CListViewHolder;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.C2CConversation;
import com.phpxiu.app.model.msg.C2CMsg;
import com.phpxiu.app.utils.KKYUtil;

import java.util.List;

/**
 * Created by 吴金洲 on 2016/7/25
 */
public class C2CListAdapter extends BaseAdapter<C2CConversation> {
    public C2CListAdapter(Context context, List<C2CConversation> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        C2CConversation c=data.get(position);
        C2CListViewHolder holder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.c2c_conersation_list_item_view,null);
            holder=new C2CListViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder= (C2CListViewHolder) convertView.getTag();
        holder.setC2CConversation(c);
        holder.headView.setImageURI(Uri.parse(c.getAvatar()));
        holder.nickView.setText(c.getNick());
        String sex=c.getSex();
        if("1".equals(sex)){
            holder.sexView.setImageResource(R.mipmap.user_sex_icon_male);
        }else{
            holder.sexView.setImageResource(R.mipmap.user_sex_icon_female);
        }
        if(!"".equals(c.getLv())){
            //holder.lvlView.setVisibility(View.VISIBLE);
            //holder.lvlView.setImageURI(Uri.parse("asset:///lvIcons/" + c.getLv() + ".png"));
        }else{
            //holder.lvlView.setVisibility(View.INVISIBLE);
        }
        KKYUtil.log("最后一条消息时间：" + c.getLastMessageTime());
        holder.timeView.setText(C2CMsg.getTimeStr(c.getLastMessageTime()));
        if(c.getLastMessageSummary()!=null&&!"".equals(c.getLastMessageSummary())){
            if(holder.msgView.getVisibility()!=View.VISIBLE){
                holder.msgView.setVisibility(View.VISIBLE);
            }
            holder.msgView.setText(c.getLastMessageSummary());
        }else{
            if(holder.msgView.getVisibility()==View.VISIBLE){
                holder.msgView.setVisibility(View.GONE);
            }
        }
        if(c.getUnReadNum()>0){
            if(holder.countView.getVisibility()!=View.VISIBLE){
                holder.countView.setVisibility(View.VISIBLE);
            }
            holder.countView.setText(c.getUnReadNum()+"");
        }else{
            if(holder.countView.getVisibility()==View.VISIBLE){
                holder.countView.setVisibility(View.GONE);
            }
        }
        if(data.size()>0){//最后一条不显示分割线
           if(position==data.size()-1){
               if(holder.divider.getVisibility()==View.VISIBLE){
                   holder.divider.setVisibility(View.INVISIBLE);
               }
           }else{
               if(holder.divider.getVisibility()!=View.VISIBLE){
                   holder.divider.setVisibility(View.VISIBLE);
               }
           }
        }
        return convertView;
    }
}
